//index.js

import {URI_PARTYWORDINDEXLIST} from "../../utils/api";
import {ajax} from "../../utils/httpRequest";

Page({
  data: {
    sw: true,
    p: 1,
    isLoading: {
      status: 0,
      txt: '加载更多...'
    },
    backRefresh: false
  },
  onLoad: function () {
    this.initData()
  },
  initData() {
    ajax.post(URI_PARTYWORDINDEXLIST, {
      uid: wx.getStorageSync('uid'),
      p: 1,
      pagesize: 10,
      upcoming:'upcoming'
    }).then(res => {
      this.setData({
        p: 1,
        sw:true,
        'isLoading.txt': '加载更多...'
      });
      this.setData({
        list: res.data
      });
      if(!this.data.list.length>0){
        this.setData({
          show_no_data:true
        })
      }else {
        this.setData({
          show_no_data:false
        })
      }
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    })
  },
  getMoreList() {
    ajax.post(URI_PARTYWORDINDEXLIST, {
      uid: wx.getStorageSync('uid'),
      p: this.data.p,
      pagesize: 10,
      upcoming:'upcoming'
    }).then(res => {
      if (res.data.length > 0) {
        this.setData({
          list: [...this.data.list, ...res.data]
          //list:this.data.list.concat(res.data.data)
        })
      } else {
        this.setData({
          sw: false,
          'isLoading.txt': '暂无更多数据'
        })
      }
    })
  },
  onShow() {
    if (this.data.backRefresh) {
      this.goTop();
      this.setData({
        p: 1,
        sw:true,
        'isLoading.txt': '加载更多...'
      });
      this.initData()
    }
  },
  goTop: function (e) {  // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  goToDetails(e) {
    this.setData({
      backRefresh: true
    });
    if (this.data.backRefresh) {
      wx.navigateTo({
        url: '../details/details?item=' + JSON.stringify(e.currentTarget.dataset.item),
      })
    }

  },
  onReachBottom: function () {
    this.setData({
      p: this.data.p + 1
    });
    if (this.data.sw) {
      this.getMoreList()
    }
  },
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.initData();
  },
});